<?php

namespace App\Http\Controllers\Api\V1;

use DB;
use Illuminate\Http\Request;

class TemplateAreaDataController extends BaseController
{

    public function show(Request $request, $spid, $termid, $template_type, $template_no)
    {

        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID不能为空!']);
        }
        if (empty($template_type)) {
            return response()->json(['status' => '404', 'data' => '模板类型不能为空!']);
        }
        if (empty($template_no)) {
            return response()->json(['status' => '404', 'data' => '模板编号不能为空!']);
        }
        // FTP 配置
        $ftpUrl = config('app.host.ftp');

        $template_type = strtolower($template_type);
        // // 根据区位模板机器编号获取具体的区位编号
        $areas = DB::table('template_area_datas')
            ->where(['template_type' => $template_type, 'template_no' => $template_no])
            ->pluck('area_no')
            ->unique();
        $data        = [];
        $androidData = [];
        if (empty($areas->first())) {
            return response()->json(['status' => '404', 'data' => '此区位模板没有数据!']);
        }
        foreach ($areas as $key => $value) {
            // 根据区位模板类型,编号,及具体区位编号按照顺序获取数据
            $templateAreaData = DB::table('template_area_datas')
                ->where([
                    'template_type' => $template_type,
                    'template_no'   => $template_no,
                    'area_no'       => $value,
                ])
                ->orderBy('tag_no')
                ->get();
            foreach ($templateAreaData as $areaKey => $areaValue) {
                switch ($areaValue->resource_type) {
                    case '1':
                        $vchan = DB::table('vchans')->where(['vchan_id' => $areaValue->resource_id])->first();
                        if (!empty($vchan)) {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = $vchan->title;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['subtitle'] = $vchan->subtitle;
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tags'] = explode('&', $vchan->tags);
                            // 图片 根据区位编号和具体区位编号查找相应图片
                            $templateImg = DB::table('template_area_images')->where(['area_no' => $value, 'tag_no' => $areaValue->tag_no])->first();
                            if (empty($templateImg)) {
                                $data['webData'][$value]['data'][$areaKey]['img'] = (object) [];
                            } else {
                                $img                                              = $templateImg->img;
                                $data['webData'][$value]['data'][$areaKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                            }
                        } else {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = null;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['subtitle'] = null;
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tag'] = null;
                            // 图片
                            $data['webData'][$value]['data'][$areaKey]['img'] = (object) [];
                        }
                        // 资源ID
                        $data['webData'][$value]['data'][$areaKey]['resource_id'] = $areaValue->resource_id;
                        // 资源类型
                        $data['webData'][$value]['data'][$areaKey]['resource_type'] = $areaValue->resource_type;
                        // 具体模板编号
                        $data['webData'][$value]['data'][$areaKey]['tag_no'] = $areaValue->tag_no;
                        // 区位模板焦点事件
                        $resource = $areaValue->resource_id;
                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplType'] = 'VLIVEDETAIL';
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplID']   = $areaValue->template_no;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['href'] = url("/{$spid}/{$termid}/vchan/{$resource}/meta");

                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplType'] = 'VLIVEDETAIL';
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplID']   = $areaValue->template_no;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onclick']['href'] = url("/{$spid}/{$termid}/vchan/{$resource}/meta");

                        $data['webData'][$value]['data'][$areaKey]['status'] = $value;
                        $androidData[]                                       = $data['webData'][$value]['data'][$areaKey];
                        break;
                    case '2':
                        $album = DB::table('albums')->where(['album_id' => $areaValue->resource_id])->first();
                        if ($album) {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = $album->title;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['subtitle'] = $album->subtitle;
                            // 名称
                            $data['webData'][$value]['data'][$areaKey]['name'] = $album->name;
                            // 图片 根据区位编号和具体区位编号查找相应图片
                            $templateImg = DB::table('template_area_images')->where(['area_no' => $value, 'tag_no' => $areaValue->tag_no])->first();
                            if (empty($templateImg)) {
                                $data['webData'][$value]['data'][$areaKey]['img'] = (object) [];
                            } else {
                                $img                                              = $templateImg->img;
                                $data['webData'][$value]['data'][$areaKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                            }
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tags'] = null;
                        } else {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = null;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['subtitle'] = null;
                            // 名称
                            $data['webData'][$value]['data'][$areaKey]['name'] = null;
                            // 图片
                            $data['webData'][$value]['data'][$areaKey]['img'] = (object) [];
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tags'] = null;
                        }
                        // 资源ID
                        $data['webData'][$value]['data'][$areaKey]['resource_id'] = $areaValue->resource_id;
                        // 资源类型
                        $data['webData'][$value]['data'][$areaKey]['resource_type'] = $areaValue->resource_type;
                        // 具体模板编号
                        $data['webData'][$value]['data'][$areaKey]['tag_no'] = $areaValue->tag_no;
                        // 区位模板焦点事件
                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplType'] = "SUBJECT";
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplID']   = $areaValue->template_no;
                        $resource                                                                 = $areaValue->resource_id;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['href'] = url("/{$spid}/{$termid}/album/{$resource}");

                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplType'] = "SUBJECT";
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplID']   = $areaValue->template_no;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onclick']['href'] = url("/{$spid}/{$termid}/album/{$resource}");
                        $data['webData'][$value]['data'][$areaKey]['status']          = $value;
                        $androidData[]                                                = $data['webData'][$value]['data'][$areaKey];
                        break;
                    case '3':
                        $recommend = DB::table('recommends')->where(['recommend_id' => $areaValue->resource_id])->first();
                        if (!empty($recommend)) {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = $recommend->title;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['subtitle'] = $recommend->subtitle;
                            // 名称
                            $data['webData'][$value]['data'][$areaKey]['name'] = $recommend->name;
                            // 图片 根据区位编号和具体区位编号查找相应图片
                            $templateImg = DB::table('template_area_images')->where(['area_no' => $value, 'tag_no' => $areaValue->tag_no])->first();
                            if (empty($templateImg)) {
                                $data['webData'][$value]['data'][$areaKey]['img'] = (object) [];
                            } else {
                                $img                                              = $templateImg->img;
                                $data['webData'][$value]['data'][$areaKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                            }
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tags'] = null;
                        } else {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = null;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['subtitle'] = null;
                            // 名称
                            $data['webData'][$value]['data'][$areaKey]['name'] = null;
                            // 图片
                            $data['webData'][$value]['data'][$areaKey]['img'] = null;
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tags'] = null;
                        }

                        // 资源ID
                        $data['webData'][$value]['data'][$areaKey]['resource_id'] = $$areaValue->resource_id;
                        // 资源类型
                        $data['webData'][$value]['data'][$areaKey]['resource_type'] = $areaValue->resource_type;
                        // 具体模板编号
                        $data['webData'][$value]['data'][$areaKey]['tag_no'] = $areaValue->tag_no;
                        // 区位模板焦点事件
                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplType'] = $areaValue->template_type;
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplID']   = $areaValue->template_no;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['href'] = null;

                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplType'] = $areaValue->template_type;
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplID']   = $areaValue->template_no;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onclick']['href'] = null;
                        $data['webData'][$value]['data'][$areaKey]['status']          = $value;
                        $androidData[]                                                = $data['webData'][$value]['data'][$areaKey];
                        break;
                    case '4':
                        $vasseti = DB::table('vassetis')->where(['vasseti_id' => $areaValue->resource_id])->first();
                        if (!empty($vasseti)) {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = $vasseti->title;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['subtitle'] = $vasseti->subtitle;
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tags'] = DB::table('vasseti_vcattags')->where('vasseti_vcattags.vasseti_id', $areaValue->resource_id)->join('vcattags', 'vasseti_vcattags.vcattag_id', '=', 'vcattags.vcattag_id')->pluck('vcattags.name')->implode(',', 'name');
                            // 图片 根据区位编号和具体区位编号查找相应图片
                            $templateImg = DB::table('template_area_images')->where(['area_no' => $value, 'tag_no' => $areaValue->tag_no])->first();
                            if (empty($templateImg)) {
                                $data['webData'][$value]['data'][$areaKey]['img'] = (object) [];
                            } else {
                                $img                                              = $templateImg->img;
                                $data['webData'][$value]['data'][$areaKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                            }
                        } else {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = null;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['subtitle'] = null;
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tag'] = null;
                            // 图片
                            $data['webData'][$value]['data'][$areaKey]['img'] = (object) [];
                        }
                        // 资源ID
                        $data['webData'][$value]['data'][$areaKey]['resource_id'] = $areaValue->resource_id;
                        // 资源类型
                        $data['webData'][$value]['data'][$areaKey]['resource_type'] = $areaValue->resource_type;
                        // 具体模板编号
                        $data['webData'][$value]['data'][$areaKey]['tag_no'] = $areaValue->tag_no;
                        // 区位模板焦点事件
                        $resource = $areaValue->resource_id;
                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplType'] = 'DETAIL';
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplID']   = $areaValue->template_no;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['href'] = url("/{$spid}/{$termid}/vasset/{$resource}/meta");

                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplType'] = 'DETAIL';
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplID']   = $areaValue->template_no;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onclick']['href'] = url("/{$spid}/{$termid}/vasset/{$resource}/meta");
                        $data['webData'][$value]['data'][$areaKey]['status']          = $value;
                        $androidData[]                                                = $data['webData'][$value]['data'][$areaKey];
                        break;
                    case '5':
                        // 栏目列表类型 1.点播栏目列表 2.直播栏目列表 3回看栏目列表
                        $collist_id = $areaValue->collist_id;
                        if ($collist_id == 1) {
                            $collist_type = "VODCOLUMN";
                        } elseif ($collist_id == 2) {
                            $collist_type = "LIVECOLUMN";
                        } elseif ($collist_id == 3) {
                            $collist_type = "REVIEWCOLUMN";
                        } else {
                            $collist_id   = 1;
                            $collist_type = "COLUMN";
                        }
                        $column = DB::table('columns')->where(['columnid' => $areaValue->resource_id])->first();
                        if (!empty($column)) {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = $column->title;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['subtitle'] = $column->subtitle;
                            // 图片 根据区位编号和具体区位编号查找相应图片
                            $templateImg = DB::table('template_area_images')->where(['area_no' => $value, 'tag_no' => $areaValue->tag_no])->first();
                            if (empty($templateImg)) {
                                $data['webData'][$value]['data'][$areaKey]['img'] = (object) [];
                            } else {
                                $img                                              = $templateImg->img;
                                $data['webData'][$value]['data'][$areaKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                            }
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tags'] = null;
                        } else {
                            // 标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = null;
                            // 副标题
                            $data['webData'][$value]['data'][$areaKey]['title'] = null;
                            // 图片
                            $data['webData'][$value]['data'][$areaKey]['img'] = (object) [];
                            // 分类
                            $data['webData'][$value]['data'][$areaKey]['tags'] = null;
                        }

                        // 具体模板编号
                        $data['webData'][$value]['data'][$areaKey]['tag_no'] = $areaValue->tag_no;
                        // 资源ID
                        $data['webData'][$value]['data'][$areaKey]['resource_id'] = $areaValue->resource_id;
                        // 资源类型
                        $data['webData'][$value]['data'][$areaKey]['resource_type'] = $areaValue->resource_type;
                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplType'] = $collist_type;
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['route']['tplID']   = $areaValue->template_no;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onfocus']['href'] = url("/{$spid}/{$termid}/column/{$collist_id}/list0");
                        // 当前模板类型,当前模板类型编号
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplType'] = $collist_type;
                        $data['webData'][$value]['data'][$areaKey]['onclick']['route']['tplID']   = $areaValue->template_no;
                        // 直播目前走点播的路由
                        $data['webData'][$value]['data'][$areaKey]['onclick']['href'] = url("/{$spid}/{$termid}/column/{$collist_id}/list0");

                        $data['webData'][$value]['data'][$areaKey]['status'] = $value;
                        $androidData[]                                       = $data['webData'][$value]['data'][$areaKey];
                        break;
                    default:
                        break;
                }
            }

            // 根据资源数判断type是tag还是ul
            if (count($templateAreaData) > 1) {
                $data['webData'][$value]["type"] = "ul";
            } else {
                $data['webData'][$value]['type'] = "tag";
            }
        }

        if ($areas->first()) {
            // 五套模板获取不同的数据
            if ($template_type == "area" && $template_no == "001") {
                $data['webData']['route']['x0y0']['onleft']  = 'boarder';
                $data['webData']['route']['x0y0']['onright'] = 'x1';
                $data['webData']['route']['x0y0']['onup']    = 'boarder';
                $data['webData']['route']['x0y0']['ondown']  = 'x0y1';

                $data['webData']['route']['x0y1']['onleft']  = 'boarder';
                $data['webData']['route']['x0y1']['onright'] = 'x1';
                $data['webData']['route']['x0y1']['onup']    = 'x0y0';
                $data['webData']['route']['x0y1']['ondown']  = 'boarder';

                $data['webData']['route']['x1']['onleft']  = 'x0y0';
                $data['webData']['route']['x1']['onright'] = 'x2';
                $data['webData']['route']['x1']['onup']    = 'boarder';
                $data['webData']['route']['x1']['ondown']  = 'boarder';

                $data['webData']['route']['x2']['onleft']  = 'x1';
                $data['webData']['route']['x2']['onright'] = 'boarder';
                $data['webData']['route']['x2']['onup']    = 'boarder';
                $data['webData']['route']['x2']['ondown']  = 'boarder';
            } else if ($template_type == "area" && $template_no == "002") {
                $data['webData']['route']['y0x0']['onleft']  = 'boarder';
                $data['webData']['route']['y0x0']['onright'] = 'y0x1';
                $data['webData']['route']['y0x0']['onup']    = 'boarder';
                $data['webData']['route']['y0x0']['ondown']  = 'y1';

                $data['webData']['route']['y0x1']['onleft']  = 'y0x0';
                $data['webData']['route']['y0x1']['onright'] = 'border';
                $data['webData']['route']['y0x1']['onup']    = 'border';
                $data['webData']['route']['y0x1']['ondown']  = 'y1';

                $data['webData']['route']['y1']['onleft']  = 'border';
                $data['webData']['route']['y1']['onright'] = 'y0x0';
                $data['webData']['route']['y1']['onup']    = 'boarder';
                $data['webData']['route']['y1']['ondown']  = 'boarder';
            } else if ($template_type == "area" && $template_no == "003") {
                $data['webData']['route']['x0y0']['onleft']  = 'boarder';
                $data['webData']['route']['x0y0']['onright'] = 'x1y0x0';
                $data['webData']['route']['x0y0']['onup']    = 'boarder';
                $data['webData']['route']['x0y0']['ondown']  = 'x0y1';

                $data['webData']['route']['x0y1']['onleft']  = 'boarder';
                $data['webData']['route']['x0y1']['onright'] = 'x1y1x0';
                $data['webData']['route']['x0y1']['onup']    = 'x0y0';
                $data['webData']['route']['x0y1']['ondown']  = 'boarder';

                $data['webData']['route']['x1y0x0']['onleft']  = 'x0y0';
                $data['webData']['route']['x1y0x0']['onright'] = 'x1y0x1';
                $data['webData']['route']['x1y0x0']['onup']    = 'boarder';
                $data['webData']['route']['x1y0x0']['ondown']  = 'x1y1x0';

                $data['webData']['route']['x1y0x1']['onleft']  = 'x1y0x0';
                $data['webData']['route']['x1y0x1']['onright'] = 'boarder';
                $data['webData']['route']['x1y0x1']['onup']    = 'boarder';
                $data['webData']['route']['x1y0x1']['ondown']  = 'x1y1x1';

                $data['webData']['route']['x1y1x0']['onleft']  = 'x0y0';
                $data['webData']['route']['x1y1x0']['onright'] = 'x1y1x1';
                $data['webData']['route']['x1y1x0']['onup']    = 'x1y0x0';
                $data['webData']['route']['x1y1x0']['ondown']  = 'boarder';

                $data['webData']['route']['x1y1x1']['onleft']  = 'x1y1x0';
                $data['webData']['route']['x1y1x1']['onright'] = 'boarder';
                $data['webData']['route']['x1y1x1']['onup']    = 'x1y0x1';
                $data['webData']['route']['x1y1x1']['ondown']  = 'boarder';
            } else if ($template_type == "area" && $template_no == "004") {
                $data['webData']['route']['x0y0']['onleft']  = 'boarder';
                $data['webData']['route']['x0y0']['onright'] = 'x1y0';
                $data['webData']['route']['x0y0']['onup']    = 'boarder';
                $data['webData']['route']['x0y0']['ondown']  = 'x0y1';

                $data['webData']['route']['x1y0']['onleft']  = 'x0y0';
                $data['webData']['route']['x1y0']['onright'] = 'x2y0';
                $data['webData']['route']['x1y0']['onup']    = 'boarder';
                $data['webData']['route']['x1y0']['ondown']  = 'x1y1';

                $data['webData']['route']['x2y0']['onleft']  = 'x1y0';
                $data['webData']['route']['x2y0']['onright'] = 'x2y0';
                $data['webData']['route']['x2y0']['onup']    = 'boarder';
                $data['webData']['route']['x2y0']['ondown']  = 'x2y1';

                $data['webData']['route']['x3y0']['onleft']  = 'x2y0';
                $data['webData']['route']['x3y0']['onright'] = 'boarder';
                $data['webData']['route']['x3y0']['onup']    = 'boarder';
                $data['webData']['route']['x3y0']['ondown']  = 'x3y1';

                $data['webData']['route']['x0y1']['onleft']  = 'boarder';
                $data['webData']['route']['x0y1']['onright'] = 'x1y1';
                $data['webData']['route']['x0y1']['onup']    = 'x0y0';
                $data['webData']['route']['x0y1']['ondown']  = 'boarder';

                $data['webData']['route']['x1y1']['onleft']  = 'x0y0';
                $data['webData']['route']['x1y1']['onright'] = 'boarder';
                $data['webData']['route']['x1y1']['onup']    = 'boarder';
                $data['webData']['route']['x1y1']['ondown']  = 'boarder';

                $data['webData']['route']['x2y1']['onleft']  = 'x1y1';
                $data['webData']['route']['x2y1']['onright'] = 'x3y1';
                $data['webData']['route']['x2y1']['onup']    = 'x2y0';
                $data['webData']['route']['x2y1']['ondown']  = 'boarder';

                $data['webData']['route']['x3y1']['onleft']  = 'x2y1';
                $data['webData']['route']['x3y1']['onright'] = 'boarder';
                $data['webData']['route']['x3y1']['onup']    = 'x3y0';
                $data['webData']['route']['x3y1']['ondown']  = 'boarder';
            }
        }

        if (empty($data)) {
            $data = (object) [];
        }

        // 安卓获取数据
        if (!empty($androidData)) {
            $data['androidData'] = $androidData;
        }

        return response()->json(['status' => '200', 'data' => $data]);
    }

    public function oldShow(Request $request, $spid, $termid, $template_type, $template_no)
    {
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID不能为空!']);
        }
        if (empty($template_type)) {
            return response()->json(['status' => '404', 'data' => '模板类型不能为空!']);
        }
        if (empty($template_no)) {
            return response()->json(['status' => '404', 'data' => '模板编号不能为空!']);
        }
        // FTP 配置
        $ftpUrl = config('app.host.ftp');

        $template_type = strtolower($template_type);

        $templateAreaData = DB::table('template_area_datas')
            ->select('area_no', DB::raw('group_concat(resource_type,"|",resource_id,"|",tag_no) as resource'))
            ->where(['template_type' => $template_type, 'template_no' => $template_no])
            ->groupBy('area_no')
            ->get();
        $data = [];
        foreach ($templateAreaData as $templateKey => $templateValue) {
            // 遍历此区位下的资源信息
            $resource = explode(',', $templateValue->resource);
            foreach ($resource as $resourceKey => $resourceValue) {
                $resourceDetails = explode('|', $resourceValue);
                if (count($resourceDetails) == 3) {
                    // 每个资源类型以及资源Id
                    $resource_type  = $resourceDetails[0];
                    $resource_value = $resourceDetails[1];
                    $tag_no         = $resourceDetails[2];
                    switch ($resource_type) {
                        // 资源类型, 1 => 直播, 2 => 专题，3 => 推荐位，4=>点播媒资 5=>栏目列表
                        case '1':
                            $vasseti = DB::table('vassetis')->where(['vasseti_id' => $resource_value])->first();
                            if (!empty($vasseti)) {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = $vasseti->title;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['subtitle'] = $vasseti->subtitle;
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tags'] = DB::table('vasseti_vcattags')->where('vasseti_vcattags.vasseti_id', $resource_value)->join('vcattags', 'vasseti_vcattags.vcattag_id', '=', 'vcattags.vcattag_id')->pluck('vcattags.name')->implode(',', 'name');
                                // 图片 根据区位编号和具体区位编号查找相应图片
                                $templateImg = DB::table('template_area_images')->where(['area_no' => $templateValue->area_no, 'tag_no' => $tag_no])->first();
                                if (empty($templateImg)) {
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = (object) [];
                                } else {
                                    $img                                                        = $templateImg->img;
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                                }
                            } else {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = null;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['subtitle'] = null;
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tag'] = null;
                                // 图片
                                $data[$templateValue->area_no]['data'][$resourceKey]['img'] = (object) [];
                            }
                            // 资源ID
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_id'] = $resource_value;
                            // 资源类型
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_type'] = $resource_type;
                            // 具体模板编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['tag_no'] = $tag_no;
                            // 区位模板焦点事件
                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplType'] = 'DETAIL';
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['href'] = url("/{$spid}/{$termid}/vasset/{$resource_value}/meta");

                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplType'] = 'DETAIL';
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['href'] = url("/{$spid}/{$termid}/vasset/{$resource_value}/meta");
                            break;
                        case '2':
                            $album = DB::table('albums')->where(['album_id' => $resource_value])->first();
                            if ($album) {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = $album->title;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['subtitle'] = $album->subtitle;
                                // 名称
                                $data[$templateValue->area_no]['data'][$resourceKey]['name'] = $album->name;
                                // 图片 根据区位编号和具体区位编号查找相应图片
                                $templateImg = DB::table('template_area_images')->where(['area_no' => $templateValue->area_no, 'tag_no' => $tag_no])->first();
                                if (empty($templateImg)) {
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = (object) [];
                                } else {
                                    $img                                                        = $templateImg->img;
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                                }
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tags'] = null;
                            } else {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = null;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['subtitle'] = null;
                                // 名称
                                $data[$templateValue->area_no]['data'][$resourceKey]['name'] = null;
                                // 图片
                                $data[$templateValue->area_no]['data'][$resourceKey]['img'] = (object) [];
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tags'] = null;
                            }
                            // 资源ID
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_id'] = $resource_value;
                            // 资源类型
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_type'] = $resource_type;
                            // 具体模板编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['tag_no'] = $tag_no;
                            // 区位模板焦点事件
                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplType'] = "SUBJECT";
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['href'] = url("/{$spid}/{$termid}/album/{$resource_value}");

                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplType'] = "SUBJECT";
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['href'] = url("/{$spid}/{$termid}/album/{$resource_value}");
                            break;
                        case '3':
                            $recommend = DB::table('recommends')->where(['recommend_id' => $resource_value])->first();
                            if (!empty($recommend)) {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = $recommend->title;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['subtitle'] = $recommend->subtitle;
                                // 名称
                                $data[$templateValue->area_no]['data'][$resourceKey]['name'] = $recommend->name;
                                // 图片 根据区位编号和具体区位编号查找相应图片
                                $templateImg = DB::table('template_area_images')->where(['area_no' => $templateValue->area_no, 'tag_no' => $tag_no])->first();
                                if (empty($templateImg)) {
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = (object) [];
                                } else {
                                    $img                                                        = $templateImg->img;
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                                }
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tags'] = null;
                            } else {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = null;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['subtitle'] = null;
                                // 名称
                                $data[$templateValue->area_no]['data'][$resourceKey]['name'] = null;
                                // 图片
                                $data[$templateValue->area_no]['data'][$resourceKey]['img'] = null;
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tags'] = null;
                            }

                            // 资源ID
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_id'] = $resource_value;
                            // 资源类型
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_type'] = $resource_type;
                            // 具体模板编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['tag_no'] = $tag_no;
                            // 区位模板焦点事件
                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplType'] = $template_type;
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['href'] = null;

                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplType'] = $template_type;
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['href'] = null;
                            break;
                        case '4':
                            $vasseti = DB::table('vassetis')->where(['vasseti_id' => $resource_value])->first();
                            if (!empty($vasseti)) {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = $vasseti->title;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['subtitle'] = $vasseti->subtitle;
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tags'] = DB::table('vasseti_vcattags')->where('vasseti_vcattags.vasseti_id', $resource_value)->join('vcattags', 'vasseti_vcattags.vcattag_id', '=', 'vcattags.vcattag_id')->pluck('vcattags.name')->implode(',', 'name');
                                // 图片 根据区位编号和具体区位编号查找相应图片
                                $templateImg = DB::table('template_area_images')->where(['area_no' => $templateValue->area_no, 'tag_no' => $tag_no])->first();
                                if (empty($templateImg)) {
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = (object) [];
                                } else {
                                    $img                                                        = $templateImg->img;
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                                }
                            } else {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = null;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['subtitle'] = null;
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tags'] = null;
                                // 图片
                                $data[$templateValue->area_no]['data'][$resourceKey]['img'] = (object) [];
                            }
                            // 资源ID
                            $data[$templateValue->area_no]['data'][$resourceKey]['row_id'] = $resource_value;
                            // 资源ID
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_id'] = $resource_value;
                            // 具体模板编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['tag_no'] = $tag_no;
                            // 资源类型
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_type'] = $resource_type;
                            // 区位模板焦点事件
                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplType'] = "DETAIL";
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['href'] = url("/{$spid}/{$termid}/vasset/{$resource_value}/meta");
                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplType'] = "DETAIL";
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['href'] = url("/{$spid}/{$termid}/vasset/{$resource_value}/meta");
                            break;
                        case '5':
                            // $collist_id = $templateValue->collist_id;
                            // 栏目列表类型 1.点播栏目列表 2.直播栏目列表 3回看栏目列表
                            $collist = DB::table('template_area_datas')->where(['area_no' => $templateValue->area_no, 'resource_id' => $resource_value])->first();
                            if ($collist) {
                                $collist_id = $collist->collist_id;
                                if ($collist_id == 1) {
                                    $collist_type = "VODCOLUMN";
                                } elseif ($collist_id == 2) {
                                    $collist_type = "LIVECOLUMN";
                                } elseif ($collist_id == 3) {
                                    $collist_type = "REVIEWCOLUMN";
                                }
                            } else {
                                $collist_id   = 1;
                                $collist_type = "COLUMN";
                            }
                            $column = DB::table('columns')->where(['columnid' => $resource_value])->first();
                            if (!empty($column)) {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = $column->title;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['subtitle'] = $column->subtitle;
                                // 图片 根据区位编号和具体区位编号查找相应图片
                                $templateImg = DB::table('template_area_images')->where(['area_no' => $templateValue->area_no, 'tag_no' => $tag_no])->first();
                                if (empty($templateImg)) {
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = (object) [];
                                } else {
                                    $img                                                        = $templateImg->img;
                                    $data[$templateValue->area_no]['data'][$resourceKey]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                                }
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tags'] = null;
                            } else {
                                // 标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = null;
                                // 副标题
                                $data[$templateValue->area_no]['data'][$resourceKey]['title'] = null;
                                // 图片
                                $data[$templateValue->area_no]['data'][$resourceKey]['img'] = (object) [];
                                // 分类
                                $data[$templateValue->area_no]['data'][$resourceKey]['tags'] = null;
                            }
                            // 具体模板编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['tag_no'] = $tag_no;
                            // 资源ID
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_id'] = $resource_value;
                            // 资源类型
                            $data[$templateValue->area_no]['data'][$resourceKey]['resource_type'] = $resource_type;
                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplType'] = $collist_type;
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onfocus']['href'] = url("/{$spid}/{$termid}/column/{$collist_id}/list0");
                            // 当前模板类型,当前模板类型编号
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplType'] = $collist_type;
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['route']['tplID']   = $template_no;
                            // 直播目前走点播的路由
                            $data[$templateValue->area_no]['data'][$resourceKey]['onclick']['href'] = url("/{$spid}/{$termid}/column/{$collist_id}/list0");
                            break;
                    }
                }
            }
            // 根据资源数判断type是tag还是ul
            if (count($resource) > 1) {
                $data[$templateValue->area_no]["type"] = "ul";
            } else {
                $data[$templateValue->area_no]['type'] = "tag";
            }
        }

        if ($templateAreaData->first()) {
            // 五套模板获取不同的数据
            if ($template_type == "area" && $template_no == "001") {
                $data['route']['x0y0']['onleft']  = 'boarder';
                $data['route']['x0y0']['onright'] = 'x1';
                $data['route']['x0y0']['onup']    = 'boarder';
                $data['route']['x0y0']['ondown']  = 'x0y1';

                $data['route']['x0y1']['onleft']  = 'boarder';
                $data['route']['x0y1']['onright'] = 'x1';
                $data['route']['x0y1']['onup']    = 'x0y0';
                $data['route']['x0y1']['ondown']  = 'boarder';

                $data['route']['x1']['onleft']  = 'x0y0';
                $data['route']['x1']['onright'] = 'x2';
                $data['route']['x1']['onup']    = 'boarder';
                $data['route']['x1']['ondown']  = 'boarder';

                $data['route']['x2']['onleft']  = 'x1';
                $data['route']['x2']['onright'] = 'boarder';
                $data['route']['x2']['onup']    = 'boarder';
                $data['route']['x2']['ondown']  = 'boarder';
            } else if ($template_type == "area" && $template_no == "002") {
                $data['route']['y0x0']['onleft']  = 'boarder';
                $data['route']['y0x0']['onright'] = 'y0x1';
                $data['route']['y0x0']['onup']    = 'boarder';
                $data['route']['y0x0']['ondown']  = 'y1';

                $data['route']['y0x1']['onleft']  = 'y0x0';
                $data['route']['y0x1']['onright'] = 'border';
                $data['route']['y0x1']['onup']    = 'border';
                $data['route']['y0x1']['ondown']  = 'y1';

                $data['route']['y1']['onleft']  = 'border';
                $data['route']['y1']['onright'] = 'y0x0';
                $data['route']['y1']['onup']    = 'boarder';
                $data['route']['y1']['ondown']  = 'boarder';
            } else if ($template_type == "area" && $template_no == "003") {
                $data['route']['x0y0']['onleft']  = 'boarder';
                $data['route']['x0y0']['onright'] = 'x1y0x0';
                $data['route']['x0y0']['onup']    = 'boarder';
                $data['route']['x0y0']['ondown']  = 'x0y1';

                $data['route']['x0y1']['onleft']  = 'boarder';
                $data['route']['x0y1']['onright'] = 'x1y1x0';
                $data['route']['x0y1']['onup']    = 'x0y0';
                $data['route']['x0y1']['ondown']  = 'boarder';

                $data['route']['x1y0x0']['onleft']  = 'x0y0';
                $data['route']['x1y0x0']['onright'] = 'x1y0x1';
                $data['route']['x1y0x0']['onup']    = 'boarder';
                $data['route']['x1y0x0']['ondown']  = 'x1y1x0';

                $data['route']['x1y0x1']['onleft']  = 'x1y0x0';
                $data['route']['x1y0x1']['onright'] = 'boarder';
                $data['route']['x1y0x1']['onup']    = 'boarder';
                $data['route']['x1y0x1']['ondown']  = 'x1y1x1';

                $data['route']['x1y1x0']['onleft']  = 'x0y0';
                $data['route']['x1y1x0']['onright'] = 'x1y1x1';
                $data['route']['x1y1x0']['onup']    = 'x1y0x0';
                $data['route']['x1y1x0']['ondown']  = 'boarder';

                $data['route']['x1y1x1']['onleft']  = 'x1y1x0';
                $data['route']['x1y1x1']['onright'] = 'boarder';
                $data['route']['x1y1x1']['onup']    = 'x1y0x1';
                $data['route']['x1y1x1']['ondown']  = 'boarder';
            } else if ($template_type == "area" && $template_no == "004") {
                $data['route']['x0y0']['onleft']  = 'boarder';
                $data['route']['x0y0']['onright'] = 'x1y0';
                $data['route']['x0y0']['onup']    = 'boarder';
                $data['route']['x0y0']['ondown']  = 'x0y1';

                $data['route']['x1y0']['onleft']  = 'x0y0';
                $data['route']['x1y0']['onright'] = 'x2y0';
                $data['route']['x1y0']['onup']    = 'boarder';
                $data['route']['x1y0']['ondown']  = 'x1y1';

                $data['route']['x2y0']['onleft']  = 'x1y0';
                $data['route']['x2y0']['onright'] = 'x2y0';
                $data['route']['x2y0']['onup']    = 'boarder';
                $data['route']['x2y0']['ondown']  = 'x2y1';

                $data['route']['x3y0']['onleft']  = 'x2y0';
                $data['route']['x3y0']['onright'] = 'boarder';
                $data['route']['x3y0']['onup']    = 'boarder';
                $data['route']['x3y0']['ondown']  = 'x3y1';

                $data['route']['x0y1']['onleft']  = 'boarder';
                $data['route']['x0y1']['onright'] = 'x1y1';
                $data['route']['x0y1']['onup']    = 'x0y0';
                $data['route']['x0y1']['ondown']  = 'boarder';

                $data['route']['x1y1']['onleft']  = 'x0y0';
                $data['route']['x1y1']['onright'] = 'boarder';
                $data['route']['x1y1']['onup']    = 'boarder';
                $data['route']['x1y1']['ondown']  = 'boarder';

                $data['route']['x2y1']['onleft']  = 'x1y1';
                $data['route']['x2y1']['onright'] = 'x3y1';
                $data['route']['x2y1']['onup']    = 'x2y0';
                $data['route']['x2y1']['ondown']  = 'boarder';

                $data['route']['x3y1']['onleft']  = 'x2y1';
                $data['route']['x3y1']['onright'] = 'boarder';
                $data['route']['x3y1']['onup']    = 'x3y0';
                $data['route']['x3y1']['ondown']  = 'boarder';
            }
        }

        if (empty($data)) {
            $data = (object) [];
        }
        return response()->json(['status' => '200', 'data' => $data]);
    }

    /**
     * 之前获取区位模板数据
     */
    public function index(Request $request, $spid, $termid, $template_type, $template_no)
    {

        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID不能为空!']);
        }
        if (empty($template_type)) {
            return response()->json(['status' => '404', 'data' => '模板类型不能为空!']);
        }
        if (empty($template_no)) {
            return response()->json(['status' => '404', 'data' => '模板编号不能为空!']);
        }
        // FTP 配置
        $ftpUrl = config('app.host.ftp');

        $template_type = strtolower($template_type);

        $templateAreaData = DB::table('template_area_datas')
            ->select('area_no', 'resource_type', 'collist_id', DB::raw('group_concat(resource_id) as resource'))
            ->where(['template_type' => $template_type, 'template_no' => $template_no])
            ->groupBy('area_no')
            ->get();
        $area  = [];
        $areas = [];
        $data  = [];
        // 服务提供商Id,默认为瀚动
        $spid = 1;
        // 根据不同区位编号取出不同的资源14  124
        foreach ($templateAreaData as $templateAreaData_key => $templateAreaData_value) {
            // 根据资源类型的不同取出相关资源
            $resource_id = explode(',', $templateAreaData_value->resource);
            switch ($templateAreaData_value->resource_type) {
                // 资源类型, 1 => 直播, 2 => 专题，3 => 推荐位，4=>点播媒资 5=>栏目列表
                case '1':
                    foreach ($resource_id as $resource_key => $resource_value) {
                        $vasseti = DB::table('vassetis')->where(['vasseti_id' => $resource_value])->first();
                        if (!empty($vasseti)) {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = $vasseti->title;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = $vasseti->subtitle;
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tags'] = DB::table('vasseti_vcattags')->where('vasseti_vcattags.vasseti_id', $resource_value)->join('vcattags', 'vasseti_vcattags.vcattag_id', '=', 'vcattags.vcattag_id')->pluck('vcattags.name')->implode(',', 'name');
                            // 图片
                            $img = json_decode($vasseti->imgs, 1);
                            if (empty($img)) {
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = (object) [];
                            } else {
                                $img[0]['href']                                                       = $ftpUrl . $img[0]['href'];
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = $img[0];
                            }
                        } else {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = null;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = null;
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tag'] = null;
                            // 图片
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = (object) [];
                        }
                        // 资源ID
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_id'] = $resource_value;
                        // 资源类型
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_type'] = $templateAreaData_value->resource_type;
                        // 区位模板焦点事件
                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplType'] = 'DETAIL';
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['href'] = url("/{$spid}/{$termid}/vasset/{$resource_value}/meta");

                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplType'] = 'DETAIL';
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['href'] = url("/{$spid}/{$termid}/vasset/{$resource_value}/meta");
                    }
                    break;
                case '2':
                    foreach ($resource_id as $resource_key => $resource_value) {
                        $album = DB::table('albums')->where(['album_id' => $resource_value])->first();
                        if (!empty($album)) {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = $album->title;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = $album->subtitle;
                            // 名称
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['name'] = $album->name;
                            // 图片
                            $img = $album->backimg;
                            if (empty($img)) {
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = (object) [];
                            } else {
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                            }
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tags'] = null;
                        } else {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = null;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = null;
                            // 名称
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['name'] = null;
                            // 图片
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = (object) [];
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tags'] = null;
                        }
                        // 资源ID
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_id'] = $resource_value;
                        // 资源类型
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_type'] = $templateAreaData_value->resource_type;
                        // 区位模板焦点事件
                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplType'] = "SUBJECT";
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['href'] = url("/{$spid}/{$termid}/album/{$resource_value}");

                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplType'] = "SUBJECT";
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['href'] = url("/{$spid}/{$termid}/album/{$resource_value}");
                    }
                    break;
                case '3':
                    foreach ($resource_id as $resource_key => $resource_value) {
                        $recommend = DB::table('recommends')->where(['recommend_id' => $resource_value])->first();
                        if (!empty($recommend)) {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = $recommend->title;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = $recommend->subtitle;
                            // 名称
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['name'] = $recommend->name;
                            // 图片
                            $img = $ftpUrl . $recommend->img;
                            if (empty($img)) {
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = (object) [];
                            } else {
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                            }
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tags'] = null;
                        } else {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = null;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = null;
                            // 名称
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['name'] = null;
                            // 图片
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = null;
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tags'] = null;
                        }

                        // 资源ID
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_id'] = $resource_value;
                        // 资源类型
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_type'] = $templateAreaData_value->resource_type;
                        // 区位模板焦点事件
                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplType'] = $template_type;
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['href'] = null;

                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplType'] = $template_type;
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['href'] = null;
                    }
                    break;
                case '4':
                    foreach ($resource_id as $resource_key => $resource_value) {
                        $vasseti = DB::table('vassetis')->where(['vasseti_id' => $resource_value])->first();
                        // dd($resource_value);
                        if (!empty($vasseti)) {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = $vasseti->title;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = $vasseti->subtitle;
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tags'] = DB::table('vasseti_vcattags')->where('vasseti_vcattags.vasseti_id', $resource_value)->join('vcattags', 'vasseti_vcattags.vcattag_id', '=', 'vcattags.vcattag_id')->pluck('vcattags.name')->implode(',', 'name');

                            // 图片
                            $img = json_decode($vasseti->imgs, 1);
                            if (empty($img)) {
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = (object) [];
                            } else {
                                $img[0]['href']                                                       = $ftpUrl . $img[0]['href'];
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = $img[0];
                            }

                        } else {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = null;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = null;
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tags'] = null;
                            // 图片
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = (object) [];
                        }
                        // 资源ID
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['row_id'] = $resource_value;
                        // 资源ID
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_id'] = $resource_value;
                        // 资源类型
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_type'] = $templateAreaData_value->resource_type;
                        // 区位模板焦点事件
                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplType'] = "DETAIL";
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['href'] = url("/{$spid}/{$termid}/vasset/{$resource_value}/meta");
                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplType'] = "DETAIL";
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['href'] = url("/{$spid}/{$termid}/vasset/{$resource_value}/meta");
                    }
                    break;
                case '5':
                    $collist_id = $templateAreaData_value->collist_id;
                    foreach ($resource_id as $resource_key => $resource_value) {
                        // 栏目列表类型 1.点播栏目列表 2.直播栏目列表 3回看栏目列表
                        if ($collist_id == 1) {
                            $template_type = "VODCOLUMN";
                        } elseif ($collist_id == 2) {
                            $template_type = "LIVECOLUMN";
                        } elseif ($collist_id == 3) {
                            $template_type = "REVIEWCOLUMN";
                        } else {
                            $template_type = "COLUMN";
                        }
                        $column = DB::table('columns')->where(['columnid' => $resource_value])->first();
                        if (!empty($column)) {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = $column->title;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = $column->subtitle;
                            // 图片
                            $img = $column->img;
                            if (empty($img)) {
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = (object) [];
                            } else {
                                $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = json_decode(json_encode(['href' => $ftpUrl . $img]));
                            }
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tags'] = null;
                        } else {
                            // 标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['title'] = null;
                            // 副标题
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['subtitle'] = null;
                            // 图片
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['img'] = (object) [];
                            // 分类
                            $data[$templateAreaData_value->area_no]['data'][$resource_key]['tags'] = null;
                        }
                        // 资源ID
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_id'] = $resource_value;
                        // 资源类型
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['resource_type'] = $templateAreaData_value->resource_type;
                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplType'] = $template_type;
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onfocus']['href'] = url("/{$spid}/{$termid}/column/{$collist_id}/list0");
                        // 当前模板类型,当前模板类型编号
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplType'] = $template_type;
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['route']['tplID']   = $template_no;
                        // 直播目前走点播的路由
                        $data[$templateAreaData_value->area_no]['data'][$resource_key]['onclick']['href'] = url("/{$spid}/{$termid}/column/{$collist_id}/list0");
                    }
                    break;
            }
            //  我的理解是用于标志一个还是多个数据，做不同的呈现
            // 根据资源数判断type是tag还是ul
            if (count($resource_id) > 1) {
                $data[$templateAreaData_value->area_no]["type"] = "ul";
            } else {
                $data[$templateAreaData_value->area_no]['type'] = "tag";
            }
        }

        if ($templateAreaData->first()) {
            // 五套模板获取不同的数据
            if ($template_type == "area" && $template_no == "001") {
                $data['route']['x0y0']['onleft']  = 'boarder';
                $data['route']['x0y0']['onright'] = 'x1';
                $data['route']['x0y0']['onup']    = 'boarder';
                $data['route']['x0y0']['ondown']  = 'x0y1';

                $data['route']['x0y1']['onleft']  = 'boarder';
                $data['route']['x0y1']['onright'] = 'x1';
                $data['route']['x0y1']['onup']    = 'x0y0';
                $data['route']['x0y1']['ondown']  = 'boarder';

                $data['route']['x1']['onleft']  = 'x0y0';
                $data['route']['x1']['onright'] = 'x2';
                $data['route']['x1']['onup']    = 'boarder';
                $data['route']['x1']['ondown']  = 'boarder';

                $data['route']['x2']['onleft']  = 'x1';
                $data['route']['x2']['onright'] = 'boarder';
                $data['route']['x2']['onup']    = 'boarder';
                $data['route']['x2']['ondown']  = 'boarder';
            } else if ($template_type == "area" && $template_no == "002") {
                $data['route']['y0x0']['onleft']  = 'boarder';
                $data['route']['y0x0']['onright'] = 'y0x1';
                $data['route']['y0x0']['onup']    = 'boarder';
                $data['route']['y0x0']['ondown']  = 'y1';

                $data['route']['y0x1']['onleft']  = 'y0x0';
                $data['route']['y0x1']['onright'] = 'border';
                $data['route']['y0x1']['onup']    = 'border';
                $data['route']['y0x1']['ondown']  = 'y1';

                $data['route']['y1']['onleft']  = 'border';
                $data['route']['y1']['onright'] = 'y0x0';
                $data['route']['y1']['onup']    = 'boarder';
                $data['route']['y1']['ondown']  = 'boarder';
            } else if ($template_type == "area" && $template_no == "003") {
                $data['route']['x0y0']['onleft']  = 'boarder';
                $data['route']['x0y0']['onright'] = 'x1y0x0';
                $data['route']['x0y0']['onup']    = 'boarder';
                $data['route']['x0y0']['ondown']  = 'x0y1';

                $data['route']['x0y1']['onleft']  = 'boarder';
                $data['route']['x0y1']['onright'] = 'x1y1x0';
                $data['route']['x0y1']['onup']    = 'x0y0';
                $data['route']['x0y1']['ondown']  = 'boarder';

                $data['route']['x1y0x0']['onleft']  = 'x0y0';
                $data['route']['x1y0x0']['onright'] = 'x1y0x1';
                $data['route']['x1y0x0']['onup']    = 'boarder';
                $data['route']['x1y0x0']['ondown']  = 'x1y1x0';

                $data['route']['x1y0x1']['onleft']  = 'x1y0x0';
                $data['route']['x1y0x1']['onright'] = 'boarder';
                $data['route']['x1y0x1']['onup']    = 'boarder';
                $data['route']['x1y0x1']['ondown']  = 'x1y1x1';

                $data['route']['x1y1x0']['onleft']  = 'x0y0';
                $data['route']['x1y1x0']['onright'] = 'x1y1x1';
                $data['route']['x1y1x0']['onup']    = 'x1y0x0';
                $data['route']['x1y1x0']['ondown']  = 'boarder';

                $data['route']['x1y1x1']['onleft']  = 'x1y1x0';
                $data['route']['x1y1x1']['onright'] = 'boarder';
                $data['route']['x1y1x1']['onup']    = 'x1y0x1';
                $data['route']['x1y1x1']['ondown']  = 'boarder';
            } else if ($template_type == "area" && $template_no == "004") {
                $data['route']['x0y0']['onleft']  = 'boarder';
                $data['route']['x0y0']['onright'] = 'x1y0';
                $data['route']['x0y0']['onup']    = 'boarder';
                $data['route']['x0y0']['ondown']  = 'x0y1';

                $data['route']['x1y0']['onleft']  = 'x0y0';
                $data['route']['x1y0']['onright'] = 'x2y0';
                $data['route']['x1y0']['onup']    = 'boarder';
                $data['route']['x1y0']['ondown']  = 'x1y1';

                $data['route']['x2y0']['onleft']  = 'x1y0';
                $data['route']['x2y0']['onright'] = 'x2y0';
                $data['route']['x2y0']['onup']    = 'boarder';
                $data['route']['x2y0']['ondown']  = 'x2y1';

                $data['route']['x3y0']['onleft']  = 'x2y0';
                $data['route']['x3y0']['onright'] = 'boarder';
                $data['route']['x3y0']['onup']    = 'boarder';
                $data['route']['x3y0']['ondown']  = 'x3y1';

                $data['route']['x0y1']['onleft']  = 'boarder';
                $data['route']['x0y1']['onright'] = 'x1y1';
                $data['route']['x0y1']['onup']    = 'x0y0';
                $data['route']['x0y1']['ondown']  = 'boarder';

                $data['route']['x1y1']['onleft']  = 'x0y0';
                $data['route']['x1y1']['onright'] = 'boarder';
                $data['route']['x1y1']['onup']    = 'boarder';
                $data['route']['x1y1']['ondown']  = 'boarder';

                $data['route']['x2y1']['onleft']  = 'x1y1';
                $data['route']['x2y1']['onright'] = 'x3y1';
                $data['route']['x2y1']['onup']    = 'x2y0';
                $data['route']['x2y1']['ondown']  = 'boarder';

                $data['route']['x3y1']['onleft']  = 'x2y1';
                $data['route']['x3y1']['onright'] = 'boarder';
                $data['route']['x3y1']['onup']    = 'x3y0';
                $data['route']['x3y1']['ondown']  = 'boarder';
            }
        }
        if (empty($data)) {
            $data = (object) [];
        }
        return response()->json(['status' => '200', 'data' => $data]);
    }
    /**
     * 获取区位模板数据
     */
    public function templateAreaDataShow($template_type, $template_no, Request $request)
    {
        // 根据区域模板信息获取全部模板数据
        // $template_type = $request->input('template_type');
        // $template_no   = $request->input('template_no');
        if (empty($template_type)) {
            return response()->json(['status' => '404', 'data' => '模板类型不能为空!']);
        }
        if (empty($template_no)) {
            return response()->json(['status' => '404', 'data' => '模板编号不能为空!']);
        }

        if (empty($templateAreaData)) {
            return response()->json(['status' => '404', 'data' => '没有相关区位模板信息!']);
        }
        $datas = [];
        foreach ($templateAreaData as $templateAreaData_key => $templateAreaData_value) {
            $data['template_no']   = $templateAreaData_value->template_no;
            $data['template_type'] = $templateAreaData_value->template_type;
            $data['resource_type'] = $templateAreaData_value->resource_type;
            $data['area_no']       = $templateAreaData_value->area_no;
            $data['resource_id']   = $templateAreaData_value->resource_id;
            // 根据资源类型和资源ID确定唯一资源
            switch ($templateAreaData_value->resource_type) {
                // 资源类型, 1 => 直播, 2 => 专题，3 => 推荐位，4=>点播媒资 5=>栏目列表
                case '1':
                    $vasseti = DB::table('vassetis')->where(['vasseti_id' => $templateAreaData_value->resource_id])->first();
                    if (!empty($vasseti)) {
                        $data['img']   = $vasseti->imgs;
                        $data['title'] = $vasseti->title;
                    } else {
                        $data['img']   = null;
                        $data['title'] = null;
                    }
                    break;
                case '2':
                    $album = DB::table('albums')->where(['album_id' => $templateAreaData_value->resource_id])->first();
                    if (!empty($album)) {
                        $data['img']   = $album->img;
                        $data['title'] = $album->name;
                    } else {
                        $data['img']   = null;
                        $data['title'] = null;
                    }
                    break;
                case '3':
                    $recommend = DB::table('recommends')->where(['recommend_id' => $templateAreaData_value->resource_id])->first();
                    if (!empty($recommend)) {
                        $data['img']   = $recommend->img;
                        $data['title'] = $recommend->name;
                    } else {
                        $data['img']   = null;
                        $data['title'] = null;
                    }
                    break;
                case '4':
                    $vasseti = DB::table('vassetis')->where(['vasseti_id' => $templateAreaData_value->resource_id])->first();
                    if (!empty($vasseti)) {
                        $data['img']   = $vasseti->imgs;
                        $data['title'] = $vasseti->title;
                    } else {
                        $data['img']   = null;
                        $data['title'] = null;
                    }
                    break;
                case '5':
                    $collist = DB::table('collists')->where(['collistid' => $templateAreaData_value->resource_id])->first();
                    if (!empty($collist)) {
                        $data['img']   = $collist->img;
                        $data['title'] = $collist->name;
                    } else {
                        $data['img']   = null;
                        $data['title'] = null;
                    }
                    break;
                default:
                    $datas = [];
                    break;
            }
            $datas[] = $data;
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }

    public function store(Request $request)
    {
    }

    public function update($id, Request $request)
    {
    }

    public function destroy($id)
    {
    }
}
